package com.woniu.config;


import com.woniu.utils.TokenPermissionInterceptor;
import com.woniu.interceptor.JWTInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Arrays;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Resource
    private TokenPermissionInterceptor tokenPermissionInterceptor;
    @Resource
    private JWTInterceptor jwtInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(
//                        "/staff/login",      // 排除登录接口
//                        "/error","/user/login", "/user/register","/user/**","/api/**",
//                        "/favicon.ico","/webjars/**","/swagger-resources/**","/v2/**"
//                        ,"/doc.html"             // 排除错误页面
//                );
//        WebMvcConfigurer.super.addInterceptors(registry);
        registry.addInterceptor(tokenPermissionInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/staff/login",      // 排除登录接口
                        "/error","/user/login", "/user/register","/user/**","/api/**",
                        "/favicon.ico","/webjars/**","/swagger-resources/**","/v2/**"
                        ,"/doc.html"             // 排除错误页面
                );

    }

//    @Resource
//    private JWTInterceptor jwtInterceptor;
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(   "/user/login", "/user/register","/book",
//                        "/favicon.ico","/webjars/**","/swagger-resources/**","/v2/**"
//                        ,"/doc.html");
//        WebMvcConfigurer.super.addInterceptors(registry);
//    }
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwtInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(   "/user/login", "/user/register","/user/**","/api/**",
//                        "/favicon.ico","/webjars/**","/swagger-resources/**","/v2/**"
//                        ,"/doc.html");
//        WebMvcConfigurer.super.addInterceptors(registry);
//    }

        @Bean
        public FilterRegistrationBean<CorsFilter> corsFilter() {
            FilterRegistrationBean<CorsFilter> registrationBean = new FilterRegistrationBean<>();
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.setAllowedHeaders(Arrays.asList("*"));
            corsConfiguration.setAllowedMethods(Arrays.asList("*"));
            corsConfiguration.setAllowedOrigins(Arrays.asList("*"));
            corsConfiguration.setMaxAge(3600L);
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", corsConfiguration);
            registrationBean.setFilter(new CorsFilter(source));
            registrationBean.setOrder(-1);
            return registrationBean;
        }



}
